package org.zdp.demo.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.zdp.common.core.domain.R;
import org.zdp.common.idempotent.annotation.RepeatSubmit;
import org.zdp.common.ratelimiter.annotation.RateLimiter;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/ok-http")
public class OkHttpTestController {

    @RepeatSubmit()
    @PostMapping("/json")
    public R<JSONObject> postJson(@RequestBody JSONObject param, HttpServletRequest request) {
        log.info("header:{}", JSONUtil.toJsonStr(request.getHeader("sign")));
        return R.ok(param);
    }

    @PostMapping("/form")
    @RateLimiter(key = "#name", time = 60, count = 10)
    public R<Object> form(@RequestParam String name, @RequestParam Integer age) {
        Map<String, Object> map = new HashMap<>();
        map.put("info", StrUtil.format("{}.{}", name, age));
        return R.ok(map);
    }
}
